﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;
using camcad7Home.CultureHelper;
namespace camcad7Home.Controllers
{
    public class TintucController : CultureController
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Tintuc/
       public ActionResult jsonList()
        {

            string a = Request.Cookies["_culture"]["lang"];
            var list = from n in db.Tintuc
                       where n.LangID==a
                       orderby n.ID descending
                       select new { n.ID,n.Tieude,n.Tomtac,n.Ngaytao,n.Hien, Ten=n.Loaitin.Ten};
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {
            
            return View();
        }

        //
        // GET: /Tintuc/Details/5

        public ActionResult Details(int id = 0)
        {
            Tintuc tintuc = db.Tintuc.Find(id);
            if (tintuc == null)
            {
                return HttpNotFound();
            }
            return View(tintuc);
        }

        //
        // GET: /Tintuc/Create

        public ActionResult Create()
        {
            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.LoaitinID = new SelectList(db.Loaitin.Where(m => m.LangID == a), "ID", "Ten");
            return View();
        }

        //
        // POST: /Tintuc/Create
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Tintuc tintuc)
        {
            if (ModelState.IsValid)
            {
                tintuc.LangID=   Request.Cookies["_culture"]["lang"];
                tintuc.Noidung = HttpUtility.HtmlEncode(tintuc.Noidung);
                tintuc.Ngaytao = DateTime.Now;
                tintuc.Luocxem = 0;
                db.Tintuc.Add(tintuc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.LoaitinID = new SelectList(db.Loaitin.Where(m => m.LangID == a), "ID", "Ten", tintuc.LoaitinID);
            return View(tintuc);
        }

        //
        // GET: /Tintuc/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tintuc tintuc = db.Tintuc.Find(id);
            if (tintuc == null)
            {
                return HttpNotFound();
            }
            string a = Request.Cookies["_culture"]["lang"];
            tintuc.Noidung = HttpUtility.HtmlDecode(tintuc.Noidung);
            ViewBag.LoaitinID = new SelectList(db.Loaitin.Where(m=>m.LangID==a), "ID", "Ten", tintuc.LoaitinID);
            return View(tintuc);
        }

        //
        // POST: /Tintuc/Edit/5
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Tintuc tintuc)
        {
            if (ModelState.IsValid)
            {
                tintuc.Noidung = HttpUtility.HtmlEncode(tintuc.Noidung);
                db.Entry(tintuc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            string a = Request.Cookies["_culture"]["lang"];
            ViewBag.LoaitinID = new SelectList(db.Loaitin.Where(m=>m.LangID==a), "ID", "Ten", tintuc.LoaitinID);
            return View(tintuc);
        }

        //
        // GET: /Tintuc/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tintuc tintuc = db.Tintuc.Find(id);
            if (tintuc == null)
            {
                return HttpNotFound();
            }
            return View(tintuc);
        }

        //
        // POST: /Tintuc/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Tintuc tintuc = db.Tintuc.Find(id);
            db.Tintuc.Remove(tintuc);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult ajDelete(int id)
        {
            Tintuc cauhinh = db.Tintuc.Where(m => m.ID == id).FirstOrDefault();
            if (cauhinh != null)
            {
                db.Tintuc.Remove(cauhinh);
                //db.SaveChanges();
                db.SaveChanges();
                return Json(new { success = true });
            }
            else
            {
                return Json(new { success = false });
            }
        }
        public ActionResult xemtintuc(int id,string tieude,string loaitin)
        {
            var tin = (from n in db.Tintuc
                       where n.ID == id
                       select n).FirstOrDefault();
            return View(tin);
        }
    }
}